define(['durandal/app', 'durandal/composition', 'i18nCommon', 'http', 'jquery', 'knockout', 'bootstrap', 'box',
        'cookie', 'css!style/login/page-style',
        'css!style/login/less'],
    function (app, composition, i18n, http, $, ko, bootstrap, box, cookie) {

        var login = new function () {
            var that = this;
            that.init = function () {
                composition.addBindingHandler("application-login", {
                    init: function (dom) {
                        that._dom = dom;
                        //that.validateSession();
                        that.bindUI();
                        that.renderUI();
                    },
                    update: function () {

                    }
                });
            };

            that.validateSession = function () {
                var validateSession = http.getInstance("main/validatesession");
                validateSession.ajax().then(function (data, result) {
                    if (data.sessionValid) {

                        cywork.global.humanId = data.human.humanId;
                        cywork.global.humanName = data.human.humanName;
                        cywork.global.dutyId = data.human.dutyId;
                        $.cookie('human_id_cookie', data.human.humanId);
                        $.cookie('human_name_cookie', data.human.humanName);
                        $.cookie('human_name_duty', data.human.dutyId);
                        app.setRoot('view/law/law', 'entrance');
                    }
                });
            };

            that.bindUI = function () {
                $('#login-btn').on('click', function (evt) {
                    that.loginHandler($(this));
                });

                /*键盘按键处理*/

                model.unKeyPressHandler = function (vm, key) {
                    if (key.keyCode === 13) {
                        $("#password").focus();
                    }
                    return true;
                };
                model.pwKeyPressHandler = function (vm, key) {
                    if (key.keyCode === 13) {
                        if (!that.isLoging) {
                            that.loginHandler();
                        }
                    }
                    return true;
                };
            };

            that.loginHandler = function (btnObject) {

                if (that.isLoging) {
                    return;
                }
                that.isLoging = true;
                btnObject = btnObject || $('#login-btn');
                btnObject.button('loading');
                setTimeout(function () {
                    if (that.loginValidater()) {
                        that.loginMain();
                    } else {
                        that.isLoging = false;
                        btnObject.button('reset');
                    }

                }, 150);
            };

            that.loginMain = function () {
                var loginHttp = http.getInstance("main/login/ajaxlogin", {type: 'POST'});
                var un = model.userName() + "";
                var pw = model.password() + "";
                un = un.base64encode();
                pw = pw.length === 0 ? "" : pw.base64encode();
                loginHttp.ajax({un: un, pw: pw}).then(function (data) {
                    $('.errorinfo-container').css('display', 'none');
                    cywork.global.humanId = data.human.humanId;
                    cywork.global.humanName = data.human.humanName;
                    cywork.global.dutyId = data.human.dutyId;
                    $.cookie('human_id_cookie', data.human.humanId);
                    $.cookie('human_name_cookie', data.human.humanName);
                    $.cookie('human_duty_cookie', data.human.dutyId);
                    app.setRoot('view/law/law', 'entrance');

                }).error(function (data, result) {
                    box.alert(result.message,"错误");
                    $('#login-btn').button('reset');
                    that.isLoging = false;
                    $('.errorinfo-container').css('display', 'block');
                });
            };

            that.loginValidater = function () {
                if (model.userName() === "") {
                    model.invalidPassword(i18n.textLoginErrorUserNameIsNull);
                    $('.errorinfo-container').css('display', 'block');
                    return false;
                }
                return true;
            };

            that.renderUI = function () {

            };
        };


        login.init();
        var model = {
            loginBtn: i18n.textLoginLoginBtn,
            invalidPassword: ko.observable(i18n.textLoginErrorPassword),
            userName: ko.observable(""),
            password: ko.observable(""),
            loginInfomation: i18n.textLoginInfomation,
            systemDevTeam: i18n.textSystemDevTeam,
            unKeyPressHandler: ko.observable(),
            pwKeyPressHandler: ko.observable(),
            img1: "style/login/images/login_bg_01.png",
            img2: "style/login/images/login_bg_02.png",
            img3: "style/login/images/login_bg_03.png"
        };
        return model;
    });